package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.TransportDepartment;
import com.hunttown.mes.rpc.domain.TransportDepartmentDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import com.hunttown.mes.rpc.exception.ConvertException;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2019-04-28 16:28:59
 */
public class TransportDepartmentConverts {

    private static Logger LOG = LoggerFactory.getLogger(TransportDepartmentConverts.class);

    public static List<TransportDepartmentDTO> convert(List<TransportDepartment> transportDepartments) {
        if (CollectionUtils.isEmpty(transportDepartments)) {
            return Collections.EMPTY_LIST;
        }

        List<TransportDepartmentDTO> transportDepartmentDTOS = Lists.newArrayList();
        for (TransportDepartment transportDepartment : transportDepartments) {
            transportDepartmentDTOS.add(convert(transportDepartment));
        }
        return transportDepartmentDTOS;
    }

    public static TransportDepartmentDTO convert(TransportDepartment transportDepartment) {
        try {
            return BeanUtils.copy(transportDepartment, TransportDepartmentDTO.class);
        } catch (Exception e) {
            LOG.error("TransportDepartment到TransportDepartmentDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportDepartment到TransportDepartmentDTO转换失败", e);
        }
    }

    public static TransportDepartment convert(TransportDepartmentDTO transportDepartmentDTO) {
        try {
            return BeanUtils.copy(transportDepartmentDTO, TransportDepartment.class);
        } catch (Exception e) {
            LOG.error("TransportDepartmentDTO到TransportDepartment转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportDepartmentDTO到TransportDepartment转换失败", e);
        }
    }
}